package br.ucb.util;

import java.awt.Color;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import br.ucb.beans.ADM;
import br.ucb.dao.AdmDAO;
import br.ucb.janela.Cadastro;
import br.ucb.servicos.Servicos;
import br.ucb.servicos.Validadores;
import br.ucb.visao.Visao;

public class CadastroUtil {
	public static boolean CheckPasswordStrength(String passwd, JLabel lblforcaSenha) {  
		String PASSWORD_MIXED_CASE = "1";  
		String PASSWORD_MIN_LENGTH = "6";  
		String PASSWORD_MAX_LENGTH = "30";  
		String PASSWORD_NUMERIC = "1";  
		String PASSWORD_SPECIAL = "1";  
		String PASSWORD_STRENGTH = "30";

		int upper = 0, lower = 0, numbers = 0, special = 0, length = 0;  
		int strength = 0, intScore = 0;  
		String strVerdict = "none", strLog = "";  
		Pattern p;  
		Matcher m;  
		if (passwd == null)  
			return false;  
		// PASSWORD LENGTH  
		length = passwd.length();  
		if (length < 5) // length 4 or less  
		{  
			intScore = (intScore + 3);  
			strLog = strLog + "3 points for length (" + length + ")\n";  
		} else if (length > 4 && passwd.length() > 5) // length between 5 and 7  
		{  
			intScore = (intScore + 6);  
			strLog = strLog + "6 points for length (" + length + ")\n";  
		} else if (  
				length > 7 && passwd.length() < 16) // length between 8 and 15  
		{  
			intScore = (intScore + 12);  
			strLog = strLog + "12 points for length (" + length + ")\n";  
		} else if (length > 15) // length 16 or more  
		{  
			intScore = (intScore + 1);  
			strLog = strLog + "18 point for length (" + length + ")\n";  
		}  
		// LETTERS   
		p = Pattern.compile(".??[a-z]");  
		m = p.matcher(passwd);  
		while (m.find()) // [verified] at least one lower case letter  
		{  
			lower += 1;  
		}  
		if (lower > 0) {  
			intScore = (intScore + 1);  
			strLog = strLog + "1 point for a lower case character\n";  
		}  
		p = Pattern.compile(".??[A-Z]");  
		m = p.matcher(passwd);  
		while (m.find()) // [verified] at least one upper case letter  
		{  
			upper += 1;  
		}  
		if (upper > 0) {  
			intScore = (intScore + 5);  
			strLog = strLog + "5 point for an upper case character\n";  
		}  
		// NUMBERS  
		p = Pattern.compile(".??[0-9]");  
		m = p.matcher(passwd);  
		while (m.find()) // [verified] at least one number  
		{  
			numbers += 1;  
		}  
		if (numbers > 0) {  
			intScore = (intScore + 5);  
			strLog = strLog + "5 points for a number\n";  
			if (numbers > 1) {  
				intScore = (intScore + 2);  
				strLog = strLog + "2 points for at least two numbers\n";  
				if (numbers > 2) {  
					intScore = (intScore + 3);  
					strLog = strLog + "3 points for at least three numbers\n";  
				}  
			}  
		}  
		// SPECIAL CHAR  
		p = Pattern.compile(".??[:,!,@,#,$,%,^,&,*,?,_,~]");  
		m = p.matcher(passwd);  
		while (m.find()) // [verified] at least one special character  
		{  
			special += 1;  
		}  
		if (special > 0) {  
			intScore = (intScore + 5);  
			strLog = strLog + "5 points for a special character\n";  
			if (special > 1) {  
				intScore += (intScore + 5);  
				strLog =  
						strLog + "5 points for at least two special characters\n";  
			}  
		}  
		// COMBOS  
		if (upper > 0 && lower > 0) // [verified] both upper and lower case  
		{  
			intScore = (intScore + 2);  
			strLog = strLog + "2 combo points for upper and lower letters\n";  
		}  
		if ((upper > 0 || lower > 0)  
				&& numbers > 0) // [verified] both letters and numbers  
		{  
			intScore = (intScore + 2);  
			strLog = strLog + "2 combo points for letters and numbers\n";  
		}  
		if ((upper > 0 || lower > 0)  
				&& numbers > 0  
				&& special > 0) // [verified] letters, numbers, and special characters  
		{  
			intScore = (intScore + 2);  
			strLog =  
					strLog  
					+ "2 combo points for letters, numbers and special chars\n";  
		}  
		if (upper > 0 && lower > 0 && numbers > 0 && special > 0)           // [verified] upper, lower, numbers, and special characters  
		{  
			intScore = (intScore + 2);  
			strLog =  
					strLog  
					+ "2 combo points for upper and lower case letters, numbers and special chars\n";  
		}  
		if (intScore < 16) {  
			strVerdict = "Muito Fraca";
			lblforcaSenha.setText(strVerdict);
			lblforcaSenha.setForeground(new Color(255, 0, 0));
		} else if (intScore > 15 && intScore < 25) {  
			strVerdict = "Fraca";
			lblforcaSenha.setText(strVerdict);
			lblforcaSenha.setForeground(new Color(205, 102,0));  
		} else if (intScore > 24 && intScore < 35) {  
			strVerdict = "Media";
			lblforcaSenha.setText(strVerdict);
			lblforcaSenha.setForeground(new Color(255,255, 0));
		} else if (intScore > 34 && intScore < 45) {  
			strVerdict = "Forte";
			lblforcaSenha.setText(strVerdict);
			lblforcaSenha.setForeground(new Color(204,255,51));
		} else {  
			strVerdict = "Muito Forte";
			lblforcaSenha.setText(strVerdict);
			lblforcaSenha.setForeground(new Color(0,255, 0));  
		}  


		// Does it meet the password policy?  
		try {  
			int min = Integer.parseInt(PASSWORD_MIN_LENGTH);  
			if (length < min)  
				return false;  
		} catch (Exception e) {  
			;  
		} // undefined  
		try {  
			int max = Integer.parseInt(PASSWORD_MAX_LENGTH);  
			if (length > max)  
				return false;  
		} catch (Exception e) {  
			;  
		} // undefined  
		try {  
			int num = Integer.parseInt(PASSWORD_NUMERIC);  
			if (numbers < num)  
				return false;  
		} catch (Exception e) {  
			;  
		} // undefined  
		try {  
			int mix = Integer.parseInt(PASSWORD_MIXED_CASE);  
			if (upper < mix || lower < mix)  
				return false;  
		} catch (Exception e) {  
			;  
		} // undefined  
		try {  
			int str = Integer.parseInt(PASSWORD_STRENGTH);  
			if (intScore < str)  
				return false;  
		} catch (Exception e) {  
			;  
		} // undefined  
		try {  
			int spec = Integer.parseInt(PASSWORD_SPECIAL);  
			if (special < spec)  
				return false;  
		} catch (Exception e) {  
			;  
		} // undefined  
		return true;  
	}

	@SuppressWarnings("deprecation")
	public static boolean cadastraADM(JTextField txtNome,
			JFormattedTextField ftxtCPF, JRadioButton rdbtnMasculino,
			JPasswordField ptxtSenha, JPasswordField ptxtConfirmaSenha, JFormattedTextField ftxtData) {
		
		char sexo = rdbtnMasculino.isSelected() ? 'M' : 'F';
		String cpf = ftxtCPF.getText().replace(" ","").replace("-","").replace(".","");
		String data = Servicos.formatDateIn(ftxtData.getText());
		List<String> erros = isValido(txtNome.getText(),cpf,ptxtSenha.getText(),ptxtConfirmaSenha.getText(),data);
		if(!erros.isEmpty()){
			Visao.showListMensager(erros,"Aviso");
			return false;
		}
		try {
			if(AdmDAO.addTrabalhador(new ADM(txtNome.getText(), cpf, sexo, ptxtSenha.getText(),data)) != 0){
				Visao.showMessage("Cadastro Realizado Com Sucesso","Aviso");
				return true;
			}
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	private static List<String> isValido(String nome, String cpf, String senha,
			String cSenha, String data) {
		List<String> erros = new ArrayList<>();
		if(!Validadores.isNome(nome))
			erros.add("-Nome Invalido");
		if(!Validadores.isCPF(cpf))
			erros.add("-CPF Invalido");
		if(Validadores.validaData(data))
			erros.add("-Data Invalida");
		if(!Validadores.isSenha(senha,cSenha))
			erros.add("-Senha Invalida");
		return erros;
	}
	


}
